﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using WebApp.Data;
using WebApp.Commons;
using System.Transactions;

namespace WebApp.Services
{
    public class ProjectAreaTypeService
    {
        public List<tbl_area_type> GetAreaType(int index,
                                      int size,
                                      string sortColumn,
                                      string sortDirection,
                                      string keyword,
                                      ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                count = db.tbl_area_types.Count(u => u.area_type_name.Contains(keyword));

                return db.tbl_area_types.Where(u => u.area_type_name.Contains(keyword)).OrderBy(sortColumn + direction).ToList();
            }
        }

        public string GetProjAreaName()
        {
            using (var db = new PAVINGDataContext())
            {

                string keyword = "ทางเดินรถ";

                return keyword;
            }
        }

        public bool Update(tbl_area_type model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    tbl_material_type eModel;

                    if (model.area_type_id == 0)
                    {
                        model.area_type_create_by = PavingContext.CurrentUserId();
                        model.area_type_create_date = DateTime.Now;

                        db.tbl_area_types.InsertOnSubmit(model);
                    }
                    else
                    {
                        eModel = db.tbl_material_types.SingleOrDefault(s => s.material_type_id.Equals(model.area_type_id));

                        if (eModel != null)
                        {
                            eModel.material_type_name = model.area_type_name;

                            eModel.material_type_update_by = PavingContext.CurrentUserId(); ;
                            eModel.material_type_update_date = DateTime.Now;
                        }
                    }

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_area_types.SingleOrDefault(s => s.area_type_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_area_types.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        #region | For SubGrid |

        public List<tbl_area_type_step> GeSteps(int typeId,
                                       int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string keyword,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var step = db.tbl_area_type_steps.Where(ats => ats.area_type_id.Equals(typeId)).OrderBy(sortColumn + direction);

                count = db.tbl_area_type_steps.Count(ats => ats.area_type_id.Equals(typeId));

                return step.ToList();
            }
        }

        public bool SubUpdate(int typeId, tbl_step model, int order)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    var eModel = new tbl_area_type_step
                                     {
                                         area_type_id = typeId,
                                         step_id = model.step_id,
                                         order_no = order,
                                         area_type_step_create_by = PavingContext.CurrentUserId(),
                                         area_type_step_create_date = DateTime.Now
                                     };

                    db.tbl_area_type_steps.InsertOnSubmit(eModel);

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool SubDelete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_area_type_steps.FirstOrDefault(s => s.area_type_step_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_area_type_steps.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        #endregion

        #region | For Inner Sub Grid |

        public List<tbl_area_type_step_material> GeStepsMatType(int typeStepId,
                                       int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string keyword,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var step = db.tbl_area_type_step_materials.Where(ats => ats.area_type_step_id.Equals(typeStepId)).OrderBy(sortColumn + direction);

                count = db.tbl_area_type_step_materials.Count(ats => ats.area_type_step_id.Equals(typeStepId));

                return step.ToList();
            }
        }

        public bool InnerSubUpdate(int typeStepId, tbl_material_type model, int qty)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    var eModel = new tbl_area_type_step_material()
                    {
                        area_type_step_id = typeStepId,
                        material_type_id = model.material_type_id,
                        used_material_quatity = qty,
                        area_type_step_materia_create_by = PavingContext.CurrentUserId(),
                        area_type_step_materia_create_date = DateTime.Now
                    };

                    db.tbl_area_type_step_materials.InsertOnSubmit(eModel);

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool InnerSubDelete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_area_type_step_materials.FirstOrDefault(s => s.area_type_step_material_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_area_type_step_materials.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        #endregion
    }
}